package org.mte.sak.util;

import java.util.EnumSet;
import java.util.Iterator;

/**
 * ClassType
 *
 * @author Marco Traversari
 * @version $Revision: 1.3 $ $Date: 2011-05-04 18:28:43 $ $Author: marco $
 */
public enum ClassType {

	STRING("String", "java.lang.String"),
	INTEGER("Integer", "java.lang.Integer"),
	DATE("Date", "java.util.Date"),
	DATE_SQL("Date", "java.sql.Date"),
	BOOLEAN_OBJ("Boolean", "java.lang.Boolean"),
	DOUBLE_OBJ("Double", "java.lang.Double"),
	FLOAT_OBJ("Double", "java.lang.Float"),
	LONG_OBJ("Long", "java.lang.Long"),
	BIG_DECIMAL("BigDecimal", "java.math.BigDecimal"),
	FILE_OBJECT ("FileObject", "it.bp.common.file.FileObject"),
	BOOLEAN("boolean", "boolean"),
	CHAR("char", "char"),
	SHORT("short", "short"),
	INT("int", "int"),
	LONG("long", "long"),
	FLOAT("float", "float"),
	DOUBLE("double", "double");

	public final String name;

	public final String def;

	private ClassType(String name, String def) {
		this.name = name;
		this.def = def;
	}

	@SuppressWarnings("rawtypes")
	public Class classObject() {
		Class classObj = null;
		try {
			if (name.equals(BOOLEAN.name)) {
				classObj = boolean.class;
			} else if (name.equals(CHAR.name)) {
				classObj = char.class;
			} else if (name.equals(SHORT.name)) {
				classObj = short.class;
			} else if (name.equals(INT.name)) {
				classObj = int.class;
			} else if (name.equals(LONG.name)) {
				classObj = long.class;
			} else if (name.equals(FLOAT.name)) {
				classObj = float.class;
			} else if (name.equals(DOUBLE.name)) {
				classObj = double.class;
			} else {
				classObj = Class.forName(def);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return classObj;
	}

	/**
	 * retrieveClassType
	 * @param className
	 * @return classType
	 */
	public static ClassType retrieveClassType(String className) {
		ClassType classType = null;
		Iterator<ClassType> iterator = EnumSet.allOf(ClassType.class).iterator();
		while (iterator.hasNext() && classType == null) {
			ClassType ct = iterator.next();
			if (ct.name.equals(className)) {
				classType = ct;
			}
		}
		return classType;
	}
}